//
//  HMStatusViewModel.swift
//  CZBKWB31
//
//  Created by Linkon Y on 16/8/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMStatusViewModel: NSObject {

    //创建一个对象
    var statusModel : HMStatusModel?
    //微博等级
    var mbrankImage : UIImage?
    //认证等级
    var verifiedImage : UIImage?
    // 微博来源
    var sinaSourceStr: String?
    // 转发
    var repostsCountStr: String?
    // 评论
    var commentsCountStr: String?
    // 赞
    var attitudesCountStr: String?
    //微博时间
    var sinaTimeStr: String?{
    
        return NSDate.getSinaDate(statusModel?.created_at ?? "").sinaDateDescription
    }
    //原创微博的正文富文本
    var orginalAttr: NSMutableAttributedString?
    //转发微博的正文富文本
    var retweetAttr:NSMutableAttributedString?
    
    
    //创建一个构造函数
    init(statusModel:HMStatusModel ) {
        super.init()
        self.statusModel = statusModel
        mbrankImage = dealVerifiedImage(statusModel.user?.mbrank ?? 0)
        verifiedImage = dealVerifiedImage(statusModel.user?.verified ?? -1)
        sinaSourceStr = dealSinaSourceStr(statusModel.source ?? "")
        repostsCountStr = getSinaCountStr(statusModel.reposts_count ?? 0, title:"转发" )
        commentsCountStr = getSinaCountStr(statusModel.comments_count ?? 0, title: "评论")
        attitudesCountStr = getSinaCountStr(statusModel.attitudes_count ?? 0, title: "赞")
        orginalAttr = sinaDealContentAttr(statusModel.text ?? "")
        retweetAttr = sinaDealContentAttr(statusModel.retweeted_status?.text ?? "")
    }
  
}

extension HMStatusViewModel{
    
    //处理微博正文的内容文本显示
    func sinaDealContentAttr(text:String) -> NSMutableAttributedString{
        //把当前的字符串专程富文本(可变富文本)
        let allTextAttr = NSMutableAttributedString(string: text)
        //正则匹配的规则
        let regex = "\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]"
        /**
         *  正则匹配规则
         *
         *  @param NSString 正则匹配规则
         - 匹配的个数
         - 匹配的结果
         - 匹配的范围
         - 匹配是否停止
         
         */
        //创建一个可变的数组
        var matchArr : [HMMatchModel] = [HMMatchModel]()
        
        (text as NSString).enumerateStringsMatchedByRegex(regex) { (count, result, range, _) in
            //实例化MOdel
            let model = HMMatchModel(string: (result.memory as! String) , range: range.memory)
            //添加模型到数组
            matchArr.append(model)
            
        }
        //倒着遍历数组
        for matchModel in matchArr.reverse(){
            
            //判断是否为NIL
            guard let emoticonModel = HMEmoticonTool.sharedTools.searchEmoticonInDefaultEmoticonsOrLxhEmoticons(matchModel.string) else{
                continue
            }
            //创建一个文字附件
            let att = NSTextAttachment()
            //设置image
            att.image =  UIImage.emoticonImage(emoticonModel)
            //行高
            let lineHeight = UIFont.systemFontOfSize(16).lineHeight
            
            att.bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
            //创建一个不可变的副文本
            let attr = NSAttributedString(attachment: att)
            //替换
            allTextAttr.replaceCharactersInRange(matchModel.range, withAttributedString: attr)
            
            
        }
        
        //返回一个替换完成后的富文本
        return allTextAttr
        
    }


}


//处理底部视图的转发、评论、赞
extension HMStatusViewModel{
    
    func getSinaCountStr(count: Int , title: String) -> String{
        if count <= 0{
            return title
        }else if count > 0 && count < 10000{
            return "\(count)"
        }else{
        let cFloat = CGFloat(count/1000)/10
        var cFloatStr = "\(cFloat)万"
         //是否包含O
            if cFloatStr.containsString(".0") {
                //字符串转换
                cFloatStr = cFloatStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            return cFloatStr
        }
    
    }

}

//处理微博来源
extension HMStatusViewModel{
    func dealSinaSourceStr(source: String) -> String{
    // 判断字符串是否包含"> 特殊字符
        if source.containsString("\">") {
            // 获取range
            let startRange = source.rangeOfString("\">")!
            let endRange = source.rangeOfString("</")!
            //字符串截取
            let  res = source.substringWithRange(startRange.endIndex..<endRange.startIndex)
            return "来自\(res)"
        }
        //为空
        return "来自 哪遥远的地方"
    }
}

//处理cell中数据的逻辑
extension HMStatusViewModel{

    //处理等级//1-6等级
    func dealmbrankImage(mbrank: Int) -> UIImage?{
        if mbrank > 0 && mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }else{
            return UIImage(named: "common_icon_membership" )
        }
    }
    
    // 认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
    func dealVerifiedImage(verified: Int) -> UIImage?{
        
        switch verified {
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl ")
        }
        
    }


}
